package handler

import (
	"github.com/gin-gonic/gin"
	"img-text-mgr/model"
	"strconv"
)

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.PostForm("page"))
	size, _ := strconv.Atoi(c.PostForm("size"))
	var article []model.Article
	offset := (page - 1) * size
	model.DB.Offset(offset).Limit(size).Find(&article)
	var total int64
	model.DB.Model(&article).Count(&total)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"list": article,
	})
	return

}
func Create(c *gin.Context) {
	img := c.PostForm("img")
	title := c.PostForm("title")
	content := c.PostForm("content")
	data := model.Article{
		Img:     img,
		Title:   title,
		Content: content,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "发布失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "发布成功",
		})
		return
	}

}
